﻿
$(document).ready(function () {
    
});

//--------添加标签begion /*12-05 覃乃林修改*/------
function addNotTag(obj) {
    var oldtag = $("#txttag").val();
    if (oldtag != "" && oldtag != $("#txttag").attr("placeholder")) {
        $("#txttag").val(oldtag + "," + $(obj).html());
    } else {
        $("#txttag").val($(obj).html());
    }
    $(obj).remove();
}
//---------end ----
$(window).keydown(function (event) {
    if (event.keyCode == 13) {
        setmap();
    }
});
$(".btnmap").click(function () 
{
    $("#div_map_body").toggle();
    $("#a_btnmap").toggle();
    if (timer == 0) {
        initialize();
        timer++;
    }
});

function setmap() {
    var place = $("#txtplace").val();
    var let = $("#txtlet").val();
    var lng = $("#txtlng").val();
    $("#div_map_body").hide(); 
    $("#a_btnmap").show();
    $("#div_note_body").show();
    $(document).scrollTop(192);
}

function selectchang(id) {
    $("#hiddseltype").val(id);
}

///=================================右侧内容
function converNum(code, num) {
    code = code.replace(/\[(.+?)\]/g, function (m, e1) {
        return "[" + num + "]";
    });
    return code;
}

function saveOrder() {
    $("#p_body").html("");
    var data = $("#left_body li").map(function () {
        var type = $(this).attr("litype");
        var result=0;
        var parant = $(this).children("div");
        var exp, exp1, exp2, id, type;
        if (type == "img") {
            exp = parant.find(".imgtitle").val();
            exp1 = parant.find(".imgfile").val();
            exp2 = parant.find(".imgaddr").val();
            picid = parant.find(".imgid").val();
            $("#p_body").append("<img style=\"max-width:580px\" src='" + exp2 + "'/><br/><span>" + exp + "</span><br/>");
        } else if (type == "txt") {
            exp = parant.find(".txtbody").val();
            exp1 = "";
            id = parant.find(".txtid").val();
            $("#p_body").append(exp + "<br/>");
        }else if (type == "mp4") {
            exp = parant.find(".mp4body").val();
            exp1 = "";
            id = parant.find(".mp4id").val();
            $("#p_body").append("<div style='width:580px;height:380px;'>" +exp + "</div><br/>");
        } else if (type == "map") {
            exp = parant.find(".mapbody").text();
            exp1 = "";
            id = parant.find(".mapid").val();
            var dd = stringToJSON(exp);
            $("#p_body").append("<div class=\"mapaddress\" lat=\"" + dd.lat + "\" lng=\"" + dd.lng + "\"><span class=\"fabu_didian\">" + dd.name + "</span></div>");
        }
        $("#left_body").children("li").each(function (xi) {
            $(this).find("input,textarea").each(function () {
                var name = $(this).attr("name");
                if (name != null && name != undefined) {
                    $(this).attr("name", converNum(name, xi));
                }
            });
        });
        return "";
    });
    //$("#list1SortOrder").val("[" + data.join(",") + "]");
    //refresh();
};
///===========================================
function doupload() {
    uploader.uploadStoredFiles();
    $("#btnupload").hide();
    $("#btnloding").show();
}

function docancel(id) {
    $("#li_" + id).remove();
    images[id] = "";
    imgaddr[id]="";
}

function dosubmitpic() {
    for (var i = 0; i < imgaddr.length; i++) {
        $("#left_body").append($("#templatenotimg").html().replaceAll("id", 0).replaceAll("img", imgaddr[i]).replaceo("filename", images[i]).replaceAll("title", "").replaceAll("picid", 0));
    }
    saveOrder();
    closemodal();
}

$(".leftdel").live("click", function () {
    $(this).parent().parent().parent().remove();
    saveOrder();
});

$(".leftdelmp3").live("click", function () {
    $(this).parent().parent().parent().remove();
    $("#mp3body").html("");
});

function openwordnotebox() {
    setmodalLoding();
    objimg = null;
    var tbody = "";
    $.post("/PublicPage/OpenNewWordNote/", { tbody: tbody }, function (html) {
        closemodal();
        $("#basic-modal-content").html(html).modal();
    });
}

function saveWord() {
    editor.sync();
    var tbody = $("#txtnote").val();
    var jjbody = tbody.trim().replace(/&nbsp;/g, "").replace(/<(.+?)>/g, "").substring(0, 20);
    if (objimg) {
        objimg.children(".txtbody").val(tbody);
        objimg.children("p").text(jjbody);
    } else {
        $("#left_body").append($("#templatenotetext").html().replaceo("body", tbody).replaceo("body", jjbody).replaceo("id", 0));
    }
    saveOrder();
    closemodal();
}

$(".txtedit").live("click", function () {
    setmodalLoding();
    objimg = $(this).parent().parent();
    var tbody = objimg.children(".txtbody").val();
    $.post("/PublicPage/OpenNewWordNote/", function (html) {
        $("#basic-modal-content").html(html).modal();
        $("#txtnote").val(tbody);
        ClosemsgModal();
    });
});

$(".aimgtitle").live("click", function () {
    var title = $(this).parent().parent().children(".imgtitle").val();
    objimg = $(this).parent().parent();
    $("#basic-modal-content").html($("#templatepictitle").html().replaceo("body", title)).modal();
});

function editpictitle() {
    var title = $("#txtpictitle").val();
    if (title.length > 30) {
        $("#span_pictitle_error").show().text("图注不允许超过20个字！");
        return;
    }
    objimg.children(".imgtitle").val(title);
    objimg.children("p").children("span").text(title);
    saveOrder();
    closemodal();
}

function openmp3box() {
    setmodalLoding();
    $("#basic-modal-content").html($("#templatemp3").html()).modal();
}

function mp3set() {
    var mp3 = $("#txtmp3").val();
    if (mp3 != "") {
        $("#hiddmp3").val(mp3);
        $("#mp3body").html($("#templatemp3play").html().replaceAll("address", mp3));
        $("#left_body").append($("#templatenotemp3").html().replaceo("body", mp3).replaceo("title", mp3.substring(0,20)));
    }
    closemodal();
    saveOrder();
}

function openmp4box() {
    setmodalLoding();
    $("#basic-modal-content").html($("#templatemp4").html()).modal();
}

function mp4set() {
    var mp4 = $("#txtmp4").val();
    if (mp4 != "") {
        $("#left_body").append($("#templatenotemp4").html().replaceo("body",mp4).replaceo("id", 0)); //decodeURI  
        saveOrder();
    }
    closemodal();
}

function refresh() {
    var initdata = $("#list1SortOrder").val();
    if (initdata == null || initdata == "") return;
    var data = stringToJSON($("#list1SortOrder").val());
    var html = "";
    $.each(data, function (i, n) {
        if (n.type == 1) {
            html += n.exp + "<br/>";
        } else if (n.type == 2) {
            html += "<img style=\"max-width:580px\" src='" + n.exp2 + "'/><br/><span>" + n.exp + "</span><br/>";
        } else if (n.type == 4) {
            html += "<div style='width:580px;height:380px;'>" + decodeURIComponent(n.exp) + "</div><br/>";
        } else if (n.type == 10) {
            var dd = stringToJSON(n.exp);
            html += "<div class=\"mapaddress\" lat=\""+dd.lat+"\" lng=\""+dd.lng+"\"><span class=\"fabu_didian\">" + dd.name + "</span></div>";
        }

    });
    $("#p_body").html(html);
    $("#p_body").ConverHtml();
}


function donotsubmit() {
    if ($("#txttitle").val() == "") {
        setmodalerrormsg("请输入游记标题！");
        $("#btndosubmit").removeAttr("disabled").val("确定");
        setTimeout("$('#txttitle').focus();gototop()", 500);
        return false;
    }

    if ($("li[litype=txt]").length == 0) {
        setmodalerrormsg("请录入游记内容！");
        $("#btndosubmit").removeAttr("disabled").val("确定");
        setTimeout("gototop()", 500);
        return false;
    }

    if ($("li[litype=img]").length == 0) {
        setmodalerrormsg("请录入一张一上的图片！");
        $("#btndosubmit").removeAttr("disabled").val("确定");
        setTimeout("gototop()", 500);
        return false;
    }

    if ($("li[litype=map]").length == 0) {
        setmodalerrormsg("请选择一个地点！");
        $("#btndosubmit").removeAttr("disabled").val("确定");
        $("#templateselectbox").dialog("destroy");
        setTimeout("gototop()",500);
        return false;
    }

    return true;
}

function gototop() {
    $("#templateselectbox").dialog("destroy");
    $('html, body').animate({ scrollTop: 0 }, 500);
}

function checktype(type) {
    var initdata = $("#list1SortOrder").val();
    if (initdata == null || initdata == "") {
        setmodalerrormsg("请录入游记内容！");
        return false;
    }
    var data = stringToJSON(initdata);
    var count = 0;
    $.each(data, function (i, n) {
        if (n.type == type) {
            count++;
        }
    });
    if (count == 0) {
        return false;
    }
    return true;
}

/*打开地址添加窗口*/
function openaddAddress() {
    $("#basic-modal-content").html($("#templateopenaddbox").html()).modal();
}
var onloading = false;
$(function () {
    $("#addCityByWrite").live("keyup", function () {
        var atext = $(this).val();
        if (!onloading) {
            onloading = true;
            $("#addCityByWrite").next("ul").children("li.content").remove();
            $.get("/action/area/GetAboutPoCoZb/", { aboutstring: atext }, function (data) {
                var html = "<li class=\"content\" onclick=\"getAboutCitys({id},{tag},'{name}',{lat},{lng})\">{name}</li>";
                var ulhtml = "";
                $.each(data, function (i, n) {
                    ulhtml += html.replaceo("id", n.Id).replaceAll("name", n.Name).replaceo("lat", n.Lat).replaceo("lng", n.Lng).replaceo("tag", n.Tag);
                });
                $("#addCityByWrite").next("ul").find(".default").before(ulhtml);
                $("#addCityByWrite").next("ul").show();
                $("#addCityByWrite").next("ul").mouseleave(function () {
                    $(this).hide();
                }).children("li.content").bind("click", function () {
                    var text = $(this).html();
                    $("#addCityByWrite").val(text);
                    $("#addCityByWrite").next("ul").hide();
                });
                onloading = false;
            });
        }
    });

    $(".mapaddress").live("mouseover", function (event) {
        var lat = $(this).attr("lat");
        var lng = $(this).attr("lng");
        var src = "http://maps.google.com/maps/api/staticmap?center=" + lat + "," + lng + "&zoom=10&size=207x190&sensor=false&markers=color:blue%7Clabel:S%7C" + lat + "," + lng;
        var html = "<img width=\"200\" src=" + src + " style=\"border:1px solid #ccc; padding:5px;\"  />";
        var p = mouseCoords(event);
        $("#basic-modal-content").html(html).css({ top: p.y, left: p.x, position: "absolute" }).show();
    }).live("mouseout", function () {
        $("#basic-modal-content").html("").removeAttr("style").hide();
    });

    $(".mapedit").live("click", function () {
        objimg = $(this).parent().parent();
        openaddAddress();
    });
});

function getAboutCitys(id, tag, name, lat, lng) {
    $("#hiddtempvaladdr").val($("#templatenotemap").html().replaceAll("map", jQuery.toJSON({ id: id, tag: tag, name: name, lat: lat, lng: lng })).replaceo("body", name).replaceo("id", 0));
}

function setAddressTobody() {
    var value = $("#hiddtempvaladdr").val();
    if (value == null || value == "" || value == undefined) {
        setmodalerrormsg("请选择目的地");
    } else {
        if (objimg) {
            objimg.parent().html($($("#hiddtempvaladdr").val()).html());
        } else {
            $("#left_body").append($("#hiddtempvaladdr").val());
            $("#hiddtempvaladdr").val('');
        }
        saveOrder();
        closemodal();
    }
}

function selectselbox() {
    $("#templateselectbox").modal({ hastitle: false, width: 200, height: 150 });
}

function showAddressBox() {
    closemodal();
    $("#basic-modal-content").html("<iframe src=\"/PublicPage/AddGoAddress/?callback=AddAddressPlace({json})\" frameborder=\"0\" scrolling=\"no\" width=\"957\" height=\"417\"></iframe>").modal();
}

function AddAddressPlace(data) {
    var html = $("#templatenotemap").html().replaceAll("map", jQuery.toJSON({ id: data.id, tag: 0, name: data.name, lat: data.lat, lng: data.lng })).replaceo("body", data.name).replaceo("id", 0);
    if (objimg) {
        objimg.parent().html($(html).html());
    } else {
        $("#left_body").append(html);
    }
    saveOrder();
    closemodal();
}

//=====================图库
function openAlbumpicbox()
{
    setmodalLoding();
    $.get("/publicpage/OpenCollectPicBox", function (html) {
        closemodal();
        $("#basic-modal-content").html(html).modal();
    });
}

function addAlbumPic(athis)
{
    var picid = $(athis).attr("data-picid");
    var address = $(athis).attr("data-img");
    var txt = $(athis).attr("data-txt");
    $("#left_body").append($("#templatenotimg").html().replaceAll("id", 0).replaceAll("img", address).replaceo("filename", "").replaceAll("title", txt).replaceAll("picid", picid));
    saveOrder();
}

function addpicinbody()
{
    closemodal();
}

function getpicsbyAlbum(sthis)
{
    var aid = $(sthis).val();
    $.get("/action/photo/GetPiclistByAlbum", { aid: aid }, function (data) {
        var html = "";
        if (data.error > 0) {
            //登录
        } else if (data) {
            $.each(data, function (i, n) {
                html += $("#templatepicalbum").html().fill(n);
            });
            $("#album-piclist").html(html);
            sethasselectalbumaaaa();
        }
    });
}

function filehaspicids()
{
   var data=$("#left_body").find("li[litype=img]").map(function () {
       return $(this).children("div").find(".imgid").val();
   }).get();
   return data;
}

function sethasselectalbumaaaa()
{
    var data = filehaspicids();
    $.each(data, function (i, n) {
        $("#album-piclist li a").each(function () {
            if (n == $(this).attr("data-picid")) {
                $(this).removeClass("canselectalbumpic");
                $(this).after("<em></em>");
            }
        });
    });
}
function newtopmenu(num) {
    $(".newmenu").children("a").eq(3).find("span").addClass("greyred");
    $(".newmenu").children("a").eq(3).find("div").attr("class", "arrowopen");
    $("#nav1").hide();
    $("#nav2").css({
        "display": "block",
        "height": "50px"
    });
    $("#nav2").find(".navlist").find("li").eq(num).addClass("newcurrent");
}
newtopmenu(0);